package net.homelinux.chaoswg.io.renga.krypto.converter;

import java.math.BigInteger;

import net.homelinux.chaoswg.io.higherorderfunctions.AbstractUnaryFunction;
import net.homelinux.chaoswg.io.renga.core.converter.Converter;
import net.homelinux.chaoswg.io.renga.krypto.datatypes.numerical.Unsigned128Bit;
import net.homelinux.chaoswg.io.renga.krypto.datatypes.numerical.UnsignedNBit;

public class StringToUNBConverter extends AbstractUnaryFunction<UnsignedNBit, String> implements Converter<UnsignedNBit, String> {

    public UnsignedNBit apply(final String arg0) throws IllegalArgumentException {
        BigInteger tmp = null;
        try {
            if (arg0.startsWith("#b")) {
                tmp = new BigInteger (arg0.substring(2), 2);
            } else if (arg0.startsWith("#o")) {
                tmp = new BigInteger (arg0.substring(2), 8);
            } else if (arg0.startsWith("#x")) {
                tmp = new BigInteger (arg0.substring(2), 16);
            } else {
                tmp = new BigInteger (arg0);
            }
        } catch (NumberFormatException nfe) {

        }
        if (tmp != null) {
            return new Unsigned128Bit (tmp);
        }
        return null;
    }
}
